package com.etc.order.provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.etc.order.common.domain.User;
import com.etc.order.common.service.UserService;
import com.etc.order.provider.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public User findUser() {
        return userMapper.getOne(1L);
    }

    @Override
    public User findByUsername(String username) {
        List<User> users = userMapper.findByUsername(username);
        return users.size() > 0 ? users.get(0) : null;
    }

    @Override
    public boolean login(String username, String password) {
        List<User> users = userMapper.findByUsername(username);
        return users.size() > 0 && users.get(0).getUserPassword().equals(password);
    }

    @Override
    public int update(User user, String fieldName) {
        String test = (String) redisTemplate.opsForValue().get("order:user:updateFor:" + fieldName + ":" + user.getUid());
        if (test != null) return 0;
        redisTemplate.opsForValue().set("order:user:updateFor:" + fieldName + ":" + user.getUid(), "test", 30, TimeUnit.DAYS);
        return userMapper.update(user);
    }
}
